package com.huan.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * 作者： huan~
 *
 * @Date 2022-04-02 14:24
 * @Description jdbc工具类
 * 读取工具类
 */
public class ConfigManager {

    private static ConfigManager configManager;
    private static Properties properties;

    //将配置文件的内容放置到properties中
    private ConfigManager()
    {
        //读取文件
        String conFile = "jdbc.properties";
        //放到新建的properties中
        properties = new Properties();
        //用当前类的类加载器加载文件；放到InputStream，
        InputStream in = ConfigManager.class.getClassLoader().getResourceAsStream(conFile);
        try {
            //将文件类放到properties中
            properties.load(in);
            if (in != null) {
                in.close();
            }
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 通过单例模式设置实例化的个数
     *
     * @return
     */
    public static ConfigManager getInstance()
    {
        //如果不为空设置一个实例对象
        if (configManager == null) {
            configManager = new ConfigManager();
        }
        return configManager;
    }

    //因为properties中放的规则是key--value，通过key获取value
    public String getString(String key)
    {
        return properties.getProperty(key);
    }
}